package cn.springcloud.fix.common.starter.sms;

import cn.springcloud.fix.common.starter.sms.aliyun.AliYunSmsProperties;
import cn.springcloud.fix.common.starter.sms.aliyun.AliYunSmsSender;
import cn.springcloud.fix.common.starter.sms.baidu.BaiduSmsProperties;
import cn.springcloud.fix.common.starter.sms.baidu.BaiduSmsSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

@Configuration
public class SmsAutoConfiguration {

    @Configuration
    @Conditional(AliYunSmsCondition.class)
    @EnableConfigurationProperties({AliYunSmsProperties.class})
    protected static class AliYunSmsConfiguration {

        @Autowired
        private AliYunSmsProperties aliyunSmsProperties;

        @Bean
        @ConditionalOnClass({AliYunSmsSender.class})
        @ConditionalOnProperty(name = "cloud.sms.type", havingValue = "aliyun")
        public SmsSender smsSender() {
            return new AliYunSmsSender(aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret());
        }
    }

    @Configuration
    @Conditional(BaiduSmsCondition.class)
    @EnableConfigurationProperties({BaiduSmsProperties.class})
    protected static class BaiduSmsConfiguration {

        @Autowired
        private BaiduSmsProperties baiduSmsProperties;

        @Bean
        @ConditionalOnProperty(name = "cloud.sms.type", havingValue = "baidu")
        public SmsSender smsSender() {
            return new BaiduSmsSender();
        }
    }

    private static class AliYunSmsCondition extends SpringBootCondition {
        @Override
        public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
            ConditionMessage.Builder message = ConditionMessage.forCondition("AliYunSms Condition");
            Environment environment = context.getEnvironment();
            String key = environment.getProperty("cloud.sms.type");
            if (StringUtils.hasText(key) && key.equals("aliyun")) {
                return ConditionOutcome.match(message.foundExactly("AliYunSms location"));
            }
            return ConditionOutcome.noMatch(message.didNotFind("AliYunSms location").atAll());
        }
    }

    private static class BaiduSmsCondition extends SpringBootCondition {
        @Override
        public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
            ConditionMessage.Builder message = ConditionMessage.forCondition("BaiduSms Condition");
            Environment environment = context.getEnvironment();
            String key = environment.getProperty("cloud.sms.type");
            if (StringUtils.hasText(key) && key.equals("baidu")) {
                return ConditionOutcome.match(message.foundExactly("BaiduSms location"));
            }
            return ConditionOutcome.noMatch(message.didNotFind("BaiduSms location").atAll());
        }
    }
}
